package edu.protbioinfo;
import java.io.IOException;
import java.io.PrintStream;
import java.text.NumberFormat;

import org.apache.commons.lang.ArrayUtils;


public class LabeledMatrix<T> extends Matrix<T> {
	private String name = "Undefined";
	private char[] rowLabels;
	private char[] colLabels;
	
	public LabeledMatrix(int rows, int cols) {
		this(Utils.getCharArrayForIncrementDigit(0, rows), Utils.getCharArrayForIncrementDigit(0, cols));
	}

	public LabeledMatrix(char[] rowLabels, char[] colLabels){
		super(ArrayUtils.getLength(rowLabels), ArrayUtils.getLength(colLabels));
		
		this.rowLabels = rowLabels;
		this.colLabels = colLabels;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public char getRowChar(int i){
		return rowLabels[i];
	}
	
	public char getColChar(int j){
		return colLabels[j];
	}
	
	public T getValue(char row, char col){
		int i = ArrayUtils.indexOf(rowLabels, row);
		int j = ArrayUtils.indexOf(colLabels, col);
		if(i < 0 || j < 0){
			throw new IllegalArgumentException("Illegal index for (" + row + ", " + col + ")");
		}
		return getValue(i, j);
	}
	
	public void setValue(char row, char col, T value){
		setValue(ArrayUtils.indexOf(rowLabels, row), ArrayUtils.indexOf(colLabels, col), value);
	}

	public String toPrettyString(){
		StringBuffer sb = new StringBuffer();
		String newLine = System.getProperty("line.separator");
		
		for(int i = 0; i < getRows(); i++){
			if(i == 0){
				sb.append("\t");
				for(int j = 0; j < getCols(); j++){
					sb.append(colLabels[j] + "\t");
				}
				sb.append(newLine);
			}
			for(int j = 0; j < getCols(); j++){
				if(j == 0){
					sb.append(rowLabels[i] + "\t");
				}
				
				T value = getValue(i, j);

				sb.append((value instanceof Double)? Utils.format((Double)value, 1) : value + "\t");
			}

			sb.append(newLine);
			sb.append(newLine);
		}
		
		return sb.toString();
	}
	
	
}
